package com.health.healthapplet.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.health.healthapplet.entity.Response;
import com.health.healthapplet.entity.TravelDeclare;
import com.health.healthapplet.entity.User;
import com.health.healthapplet.service.TravelDeclareService;
import com.health.healthapplet.utils.EasyPoiUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class TravelDeclareController {
    @Autowired
    TravelDeclareService travelDeclareService;
    //添加
    @RequestMapping(value = "/health-service/addTravelPerson",method = RequestMethod.POST)
    public Response addTravelPerson(@RequestBody TravelDeclare travelDeclare){
        if(travelDeclare.getTravelPersonName() != null&& travelDeclare.getTravelPersonGender() != null){
            List <TravelDeclare> list = travelDeclareService.queryTravelPersonName(travelDeclare.getTravelPersonName());
            if(list != null && list.size()>0){
                return new Response("申报失败,只能申报一次,请勿多次申报",400,false);
            }else{
                int count = travelDeclareService.addTravelPerson(travelDeclare);
                if(count>0){
                    return new Response("申报成功",200,true);
                }else{
                    return new Response("申报失败",400,false);
                }
            }
        }else{
            return new Response("申报失败",400,false);
        }
    }
    //列表
    @RequestMapping(value = "/health-service/travelPersonList",method = RequestMethod.GET)
    public Response queryTravelPersonList(@RequestParam("pageNum") int pageNum,@RequestParam("pageSize") int pageSize){
        Response response = new Response();
        PageHelper.startPage(pageNum,pageSize);
        List<TravelDeclare> list = travelDeclareService.queryTravelPersonList();
        Map<String, Object> map = new HashMap<>();
        map.put("list", list);
        map.put("total", new PageInfo<>(list).getTotal());
        if(list != null && list.size()>0){
            response.setResponse(true,"操作成功",200,map);
            return response;
        }else{
            return new Response("操作失败",400,false);
        }
    }
    //导出
    @RequestMapping(value = "/health-service/travelPersonExport",method = RequestMethod.POST)
    public void queryTravelPersonList(HttpServletResponse response){
        List <TravelDeclare> list = travelDeclareService.queryTravelPersonList();
        EasyPoiUtil.exportExcel(list,"申报列表-导出","申报列表", TravelDeclare.class,"申报列表",response);
    }
}
